var SyncView = Ext.extend(Ext.Panel, {
	iconCls: 'refresh', 
	title: 'Сервер',
    cls: 'syncpanel fullscroll',
    scroll: 'vertical',    
    dockedItems : [
        {xtype: 'toolbar', dock: 'top',
            items: [
                    {text: 'Connect', handler: dataSync.reconnect},
                    {xtype: 'spacer'},
                    {text: 'Download', handler: dataSync.requestDownload}
            ]
        },
    ],    
    removeOlds: function(){
        var items = this.items;
        var maxCnt = 50;
        if (items.getCount() > maxCnt) {
            while (items.getCount() > maxCnt) {
                items.last().destroy();
            }
            this.doComponentLayout();
        }
    },
    newNote: function(r, o) {
        var noteData = {
            ts: new Date().format('H:i:s'),
            label: o.label,
            status: 'S',
            dowloadSessionId: dataSync.downloadSession.id,
            filterName: o.params.filter
        };
        
        if (dataSync.downloadSession.id && o.params.filter) {
            var chunkId =  dataSync.downloadSession.id + '-' + o.params.filter;
            var oldNote = this.down('syncnote[chunkId="' + chunkId + '"]');
        
            if (oldNote) {
                oldNote.dataSafe = noteData;
                this.updateNote(r, o, o.syncNoteId = oldNote.getId(), 'C');
                return;
            }
        }

        var note = this.insert(0, new SyncNote ({
            xtype: 'syncnote',
            dataSafe: noteData,
            data: [noteData],
            chunkId: dataSync.downloadSession.id + '-' + o.params.filter
        }));
        
        this.doComponentLayout();
        
        o.syncNoteId = note.getId();
        
        this.removeOlds();
        
    },
    successNote: function(r, o) {
        o.request.status = 'D';
        this.updateNote(r, o);
    },    
    failureNote: function(r, o) {
        o.request.status = 'F';
        this.updateNote(r, o);
    },
    updateNote: function(r, o, nId, s, nd) {               
        if (nId || (nId = o.request.options.syncNoteId)) {
            note = Ext.getCmp(nId);
            note.update(Ext.applyIf({status: s || o.request.status}, note.dataSafe));
        }
    }
});

SyncNote = Ext.extend(Ext.Component, {
    cls: 'syncnote',
    tpl: '<tpl for=".">'
            + '<div class="status {status}">{status}</div>'
            + '<div class="ts">{ts}</div>'
            + '<div class="label">{label}</div>'
            + '<div class="measure">{measure}</div>'
         + '</tpl>'
});
Ext.reg('syncnote', SyncNote);